<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="checkStatus">
        <el-select v-model="queryParams.checkStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.check_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:fee:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:fee:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:fee:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="项目名称" align="center" prop="projectName"/>
      <el-table-column label="申请单位" align="center" prop="sqDw"/>
      <el-table-column label="审核状态" align="center" prop="checkStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.check_status" :value="scope.row.checkStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="checkDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核备注" align="center" prop="checkRemark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:fee:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="checkUpdate(scope.row)"
          >审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:fee:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改经费申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称"/>
        </el-form-item>
        <el-form-item label="申请单位" prop="sqDw">
          <el-input v-model="form.sqDw" placeholder="请输入申请单位"/>
        </el-form-item>
        <el-form-item label="负责人及联系方式" prop="contactFs">
          <el-input v-model="form.contactFs" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="项目介绍" prop="projectInfo">
          <el-input v-model="form.projectInfo" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="申请经费总额" prop="feeMoney">
          <el-input-number v-model="form.feeMoney" placeholder="请输入申请经费总额"/>
        </el-form-item>
        <el-form-item label="预算说明" prop="ysSm">
          <el-input v-model="form.ysSm" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <el-form ref="formfj" label-width="120px">
        <el-form-item label="附件信息" prop="ysSm">
          <div>
            <FileUpload
              v-if="open"
              ref="expertRef"
              :disabled="true"
              :bottom-tips="'文件格式必须为\'.doc\',\'.docx\',\'.pdf\',\'.png\',文件大小不超过10M'"
              :businessId="form.id"
              :biz-type="'xqfj001'"
              :limit="5"
              :accept="['.doc','.docx','.pdf','.png']"
              style="margin-left: 20px"
            ></FileUpload>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="title" :visible.sync="checkopen" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="checkrules" label-width="120px">
        <el-form-item label="审核" prop="checkStatus">
          <el-select v-model="form.checkStatus" style="width: 196.4px" placeholder="审核是否通过" clearable>
            <el-option label="通过" value="2"/>
            <el-option label="不通过" value="3"/>
          </el-select>
        </el-form-item>
        <div class="itemcol textarea">
          <el-form-item label="审核意见">
            <el-input v-model="form.checkRemark" placeholder="审核意见" type="textarea"></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="checkSubmitForm">确 定</el-button>
        <el-button @click="checkCancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listFee, getFee, delFee, addFee, updateFee } from '@/api/business/projectfee'
import FileUpload from '@/components/fileUpload_new.vue'
import { guid } from '../../../utils/utils'
import { check, saveOrUpdate } from '../../../api/business/projectfee'

export default {
  name: 'Fee',
  components: { FileUpload },
  dicts: ['check_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      checkopen :false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 经费申请表格数据
      feeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        sqDw: null,
        contactFs: null,
        projectInfo: null,
        feeMoney: null,
        ysSm: null,
        checkStatus: null,
        checkDate: null,
        checkRemark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectName: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' }
        ],
        sqDw: [
          { required: true, message: '申请单位不能为空', trigger: 'blur' }
        ],
        contactFs: [
          { required: true, message: '负责人及联系方式不能为空', trigger: 'blur' }
        ],
        projectInfo: [
          { required: true, message: '项目介绍不能为空', trigger: 'blur' }
        ],
        feeMoney: [
          { required: true, message: '申请经费总额不能为空', trigger: 'blur' }
        ]
      },
      checkrules: {
          checkStatus: [
            {required: true, message: "是否通过", trigger: "blur"}
          ],
      },

    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询经费申请列表 */
    getList() {
      this.loading = true
      listFee(this.queryParams).then(response => {
        this.feeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    checkCancel(){
      this.checkopen = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectName: null,
        sqDw: null,
        contactFs: null,
        projectInfo: null,
        feeMoney: null,
        ysSm: null,
        checkStatus: null,
        checkDate: null,
        createBy: null,
        checkRemark: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加经费申请';
      let mizId = guid().replace(/-/g, '');
      this.form.id = mizId

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getFee(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改经费申请'
      })
    },

    checkUpdate(row) {
      this.form.checkStatus='2'
      this.form.id=row.id
      this.checkopen = true;
      this.title = "审核";

    },

    checkSubmitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          check(this.form).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.checkopen = false
            this.getList()
          })
        }
      })
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
            saveOrUpdate(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除经费申请编号为"' + ids + '"的数据项？').then(function() {
        return delFee(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/fee/export', {
        ...this.queryParams
      }, `fee_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
